<%@page import="org.opengeoingest.tufts.Layer.*, org.OpenGeoPortal.Geoserver.REST.*, java.io.*, org.apache.commons.fileupload.disk.*, java.net.*,
 org.apache.commons.fileupload.*, org.apache.commons.fileupload.servlet.*, java.util.*"%>
 
<jsp:include page="includes/header.jspf" />
<%
// accept form input and delete layers with provided ids

// for reasons I don't completely understand, using exec this run the curl command doesn't work
// curl http://gis01.uit.tufts.edu/solr/update?commit=true -H "Content-Type: text/xml" --data-binary '<delete><id>GISPORTAL.GISOWNER01.BOSTONCOUNCILDISTRICTS03</id></delete>'
// the parameters don't get passed correctly

// instead, "http://" + hostname + "/solr/update?stream.body=<delete><query>LayerId:" + layerId + "</query></delete><commit/>"; 
// works

	String hostname = request.getServerName() + ":" + request.getServerPort();
	String enteredLayerIds = request.getParameter("layerIds");
	String[] layerIds = enteredLayerIds.split(" ");
	Runtime runtime = Runtime.getRuntime();
	
	
	
	for (int i = 0 ; i < layerIds.length ; i++) 
	{
		String layerId = layerIds[i].trim();
		//String temp = "http://" + hostname + "/solr/update -H \"Content-Type:text/xml\" --data-binary '<delete><query>LayerId:" + layerId + "</query></delete>'";
		//String temp = "http://" + hostname + "/solr/update?commit=true -H \"Content-Type:text/xml\" --data-binary \"<delete><query>LayerId:" + layerId + "</query></delete>\"";
		String temp = "http://" + hostname + "/solr/update?stream.body=<delete><query>LayerId:" + layerId + "</query></delete><commit/>";

		
/*		
		String postData = URLEncoder.encode("commit", "UTF-8") + "=" + URLEncoder.encode("true", "UTF-8");
	    postData += "&" + URLEncoder.encode("binary-data", "UTF-8") + "=" + URLEncoder.encode("<delete><id>" + layerId + "</id></delete>", "UTF-8");
	    // Send data
	    try
	    {
	    	String temp2 ="http://" + hostname + "/solr/update";
	    	System.out.println("  using, " + temp2);
	    	URL url = new URL(temp2);  //hostname:80/cgi");
	    	URLConnection connection = url.openConnection();
	    	connection.setRequestProperty("Content-Type", "text/xml");
	    	connection.setDoOutput(true);
	    	OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
	    	wr.write(postData);
	    	wr.flush();

		    // Get the response
	    	BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	    	String line;
	    	while ((line = rd.readLine()) != null) 
	    	{
	        	out.write(line + "<br/>");
	        	System.out.println("   " + line);
	   		}
	    	wr.close();
	    	rd.close();
		} 
	    catch (Exception e) 
	    {
	    	System.out.println("exception " + e);
		}
*/	    

/*		
		String temp = "http://" + hostname + "/solr/update?commit=true --data-binary \"<delete><id>" + layerId + "</id></delete>\"";
		URL url = new URL(temp);
	    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		connection.setDoOutput(true); // Triggers POST.
		try
		{
			connection.setRequestMethod("POST");
		}
		catch (ProtocolException e) 
		{
			System.out.println("Shouldn't happen: HttpURLConnection doesn't support POST??");
		}
		connection.setRequestProperty("Content-Type", "text/xml");
	    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	    int status = ((HttpURLConnection) connection).getResponseCode();
	    System.out.println("in solrDeleteHandler.jsp, http connection status = " + status);
	    String inputLine;
	    while ((inputLine = reader.readLine()) != null) 
	    	System.out.println(inputLine);
	    reader.close();
*/


		String deleteCommand = "curl " + temp;
		//out.write("<br/>" + deleteCommand + "<br/>");
		System.out.println("in solrDeleteHandler, running command " + deleteCommand);
		Process process = runtime.exec(deleteCommand);
		int returnValue = process.waitFor();
		if (returnValue == 0)
		{
			out.write("<br/>" + layerId + " deleted from solr.");
		}	
		else
		{
			out.write("<br/>error deleting " + layerId + " from solr, return value = " + returnValue);
		}

	}
	String temp = "http://" + hostname + "/solr/update?stream.body=<commit/>";
	String commitCommand = "curl " + temp;
	//out.write("<br/>" + commitCommand + "<br/>");
	System.out.println("in solrDeleteHandler, running command " + commitCommand);
	Process process = runtime.exec(commitCommand);
	int returnValue = process.waitFor();
	if (returnValue == 0)
	{
		out.write("<br/>solr commit succeeded"); 
	}	
	else
	{
		out.write("<br/>failure on solr commit"); 
	}
	


%>


<jsp:include page="includes/footer.jspf" />